<?php
/**                                          
 * 用户隐私设置
 * @author M2
 * @version 1.0
 * create Date 2013-04-27
 */
class UserPrivacyModel extends Model {
	/**
	 * 获取用户设置	
	 * @param int $mid 用户ID
	 */
	function getUserSet($mid) 	{
		$userPrivacy = $this->where("uid=$mid")->field('`key`,`value`')->select();
		if($userPrivacy){
			foreach ($userPrivacy as $k=>$v){
				$r[$v['key']] = $v['value'];
			}
			return $r;
		}else{
			return $this->defaultSet();
		}
	}

	/**
	 * 保存用户设置	
	 * @param array $message
	 * @param int $uid
	 */
	function dosave($message,$uid){
		if(empty($uid)){
			return false;
		}
		$map['uid'] = $uid;
		$map['key'] = 'weibo_comment';
		$data['key']   = 'weibo_comment';
		$data['value'] = $message['weibo_comment'];
		$res = $this->where($map)->find();
		if($res){
			$this->where($map)->save($data);
		}
		if(!$res){
			$data['uid'] = $uid;
			$this->add($data);
		}
		unset($map);unset($data);
		$map['uid'] = $uid;
		$map['key'] = 'message';
		$data['key']   = 'message';
		$data['value'] = $message['message'];
		$res = $this->where($map)->find();
		if($res){
			$this->where($map)->save($data);
		}
		if(!$res){
			$data['uid'] = $uid;
			$this->add($data);
		}
		unset($map);unset($data);
		$map['uid'] = $uid;
		$map['key'] = 'space';
		$data['key']   = 'space';
		$data['value'] = $message['space'];
		$res = $this->where($map)->find();
		if($res){
			$this->where($map)->save($data);
		}
		if(!$res){
			$data['uid'] = $uid;
			$this->add($data);
		}
		return true;
	}

	/**
	 * 默认配置	
	 */
	private function defaultSet(){
		return array(
			'weibo_comment' => 0, //(所有人，除黑名单)
			'message' => 0,	//(所有人，除黑名单)
			'space' => 0,
		);
	}

	/**
	 * 获取用户隐私设置	
	 * @param int $mid
	 * @param int $uid
	 */
	function getPrivacy($mid,$uid){
		if($mid==$uid) {
			$data['weibo_comment'] = true;
			$data['message']       = true;
			$data['follow']        = true;
			return $data;
		}
		$isBackList  = $this->isBlackList($uid, $mid);		
		$followState = $this->getFollowState($uid, $mid) != 'unfollow';		
		$userset     = $this->getUserSet($uid);
		if ($isBackList) {
			$data['weibo_comment'] = false;
			$data['message']       = false;
			$data['follow']        = false;
			$data['blacklist']     = true;
		}else {
			$data['weibo_comment'] = ( $userset['weibo_comment'] )? $followState : true;
			$data['message']       = ( $userset['message'] )? $followState : true;
			$data['follow']        = true;
			$data['blacklist']     = false;
		}		
		return $data;
	}
	
	/**
	 * 是否为黑名单成员
	 * M2
	 * @param int $uid 用户ID
	 * @param int $fid 好友ID
	 * @return int
	 */
	function isBlackList($uid, $fid) {
		$key = $uid . '_' . $fid;
		static $_black = array();
		if (!isset($_black[$key]))
			$_black[$key] = M('UserBlacklist')->where("uid=$uid AND fid=$fid")->find() ? '1' : '0';
	
		return $_black[$key];
	}

	/**
	 * 设置黑名单	
	 * @param int $mid
	 * @param string $type
	 * @param int $fid
	 */
	function setBlackList($mid,$type,$fid){
		if($type=='add'){
			$map['uid'] = $mid;
			$map['fid'] = $fid;
			if( M('user_blacklist')->where($map)->count()==0 ){
				$map['ctime'] = time();
				M('user_blacklist')->add($map);  //添加黑名单
				M('weibo_follow')->where("(uid=$mid AND fid=$fid) OR (uid=$fid AND fid=$mid)")->delete(); //自动解除关系
				return true;
			}else{
				return false;
			}
		}else{
			$map['uid'] = $mid;
			$map['fid'] = $fid;
			return M('user_blacklist')->where($map)->delete();
		}
	}

	/**
	 * 获取黑名单列表	
	 * @param int $mid
	 */
	function getBlackList($mid){
		$list = $this->table ( C ( 'DB_PREFIX' ) . 'user_blacklist as up' )->join ( C ( 'DB_PREFIX' ) . 'user as u on up.fid=u.uid' )->field ( 'up.*,u.uname' )->where ( "up.uid=$mid" )->select ();
		return $list;
	}
	
	/**
	 * 获取黑名单列表-手机用
	 * @param int $mid
	 */
	function getAppBlackList($mid){
		$list = $this->table ( C ( 'DB_PREFIX' ) . 'user_blacklist as up' )->join ( C ( 'DB_PREFIX' ) . 'user as u on up.fid=u.uid' )->field ( 'up.fid as userid,u.uname as username' )->where ( "up.uid=$mid" )->select ();
		return $list;
	}
	
	/**
	 * 获取关注状态
	 * M2
	 * @param int $uid 用户ID
	 * @param int $fid 好友ID
	 * @param int $type 0:关注好友 1:关注话题
	 * @return string eachfollow:相互关注 | havefollow:已关注 | unfollow:未关注 | 空:同一个用户
	 */
	function getFollowState($uid,$fid,$type=0) {
	
		if ($uid <= 0 || $fid <= 0)
			return 'unfollow';
	
		if ($type == 0) {
			if ($uid==$fid)
				return 'unfollow';
	
			if (M('weibo_follow')->where("(uid=$uid AND fid=$fid AND type=0) OR (uid=$fid AND fid=$uid AND type=0)")->count() == 2) {
				return 'eachfollow';
			}else if ( M('weibo_follow')->where("uid=$uid AND fid=$fid AND type=0")->count()) {
				return 'havefollow';
			}else {
				return 'unfollow';
			}
		}else if($type == 1) {
			$db_prefix  = C('DB_PREFIX');
			if(is_numeric($fid)){
				$fid['topic_id'] = $fid;
			}
			$map = $fid['topic_id']?"AND a.topic_id={$fid['topic_id']}":"AND a.name='{$fid['name']}'";
			$topic = M('weibo_follow')->field('a.topic_id')
			->table("{$db_prefix}weibo_topic a LEFT JOIN {$db_prefix}weibo_follow b ON b.fid=a.topic_id")
			->where("b.uid={$uid} AND b.type=1 {$map}")
			->find();
			if($topic){
				return 'havefollow';
			}else{
				return 'unfollow';
			}
		}
	}
}
?>